<?php
namespace app\admin\controller;

use think\Request;

class Common extends \think\Controller
{
    //构造函数
    public function _initialize(){
        //验证登录
        $this->checklogin();
        //检查是否有权限
        $this->checkAuth();
    }
    public function checklogin(){
        //验证是否登录
        //登录五分钟内有效
        $user_name = session('account');
        $time = session('login_time');
        if($user_name == NULL ){
            $this->error('请先登录',"admin/login/index");
        }
        //检查用户是否有权限
       if($this->checkAuth()==false){
        $this->error('你没有访问权限');
       }
        //获取菜单
          $this->getMenus();
    }

       //是否有权限
    public function checkAuth(){
        $role_ids = session('role_ids');
        $role_ids = explode(',',$role_ids);
        //dump($role_ids);die;
        $permission_list=[];
        if(in_array(1,$role_ids)){
           // echo '111';die;
        //代表超级管理员
        $hasPermission = true;
        }else{
            $permission = model('Permission');
            $permission_ids= session('permission_ids');
            //查找权限
            $permission_list = $permission->getpermission($permission_ids);
            //拿跳转地址
            $request = Request::instance();
            $module = $request->module();
            $controller = $request->controller();
            $action = $request->action();
            $mca = $module.'/'.$controller.'/'.$action;
            $this->assign('mca',$mca);
            $hasPermission = false;
            //return $permission_ids;
            foreach($permission_list as $k =>$v){
                if(strtolower($v['permission_name']) == strtolower($mca)){
                    $hasPermission = true;
                    break;
                }
            }
        }
        return $hasPermission;
    }//方法结束


    //获取菜单方法
    public function getMenus()
    {
        $permission = model('Permission');
        $menus = $permission->getMenus(session('permission_ids'));
        //dump($menus);
        $allpermission = $permission->getpermission();
        //dump($allpermission);die;
        $allmenus=[];
        foreach($menus as $k => $v){
            $allmenus[$v["name"]]=$v;
            foreach($allpermission as $k1 => $v1){
                if($v1['pid']==$v['permission_id']){
                    $allmenus[$v["name"]]['son'][]=$v1;
                }
            }
        }
        //dump($allmenus);die;
        $this->assign('allmenus',$allmenus);

    }

    //修改管理员资料
    public function admin_edit(){
        $admin_id = input('get.admin_id');
        if(empty($admin_id)){
            $this->error('请先登录','admin/login/index');
        }else{
            $admin= db('admin')->where("id = $admin_id")->find();
            //dump($admin);die;
            $this->assign('admin',$admin);
            return view();
        }
    }
    //更新管理员密码入库
    public function update_admin(){
        $admin_id = input('get.admin_id');
        $data = input('post.');
       // dump($data);die;
        $validate = validate('Admin3');
         if(!$validate->check($data)){
             $this->error($validate->getError());
         }
        $data['used_password']=md5($data['used_password']);
        $admin= db('admin')->where("id = $admin_id")->find();
        if($data['used_password']!=$admin['password']){
            $this->error('原密码不正确');
        }else if($data['used_password']==md5($data['password'])){
            $this->error('新密码不可与原密码一致');
        }else{
            $admin= db('admin')->where("id = $admin_id")->setField('password', md5($data['password']));
            if($admin){
                $this->success('密码修改成功','admin/index/index');
            }else{
                $this->error('密码修改失败');
            }
        }
        //dump($data);
    }
}
